import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Inject,
  Req,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { CommentService } from './comment.service';
import { CreateCommentDto } from './dto/create-comment.dto';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import { BlogService } from '../blog/blog.service';
import { Request } from 'express';
import { DailyService } from 'src/daily/daily.service';
import { QueryCommentDto } from './dto/query-comment.dto';
import { _BusinessTypeList } from '../utils';
// import { Request } from 'express';
@Controller('comment')
export class CommentController {
  constructor(private readonly commentService: CommentService) {}

  @Inject(BlogService)
  private blogService: BlogService;

  @Inject(DailyService)
  private dailyService: DailyService;

  @Post('getCommentById')
  async getCommentsById(
    @Body('id') id: number,
    @Body('userId') userId: number,
  ) {
    return await this.commentService.getCommentsById({ id, userId });
  }

  @Post('query')
  async getCommentByCreateId(@Body() queryParam: QueryCommentDto) {
    return await this.commentService.getByCreateId(queryParam);
  }

  // @Get('like/:id')
  // async like (@Param('id') id: number) {
  //   return await this.commentService.like(id)
  // }

  // @Get('unlike/:id')
  // async unlike (@Param('id') id: number) {
  //   return await this.commentService.unlike(id)
  // }

  @RequireLogin()
  @Post('like')
  async like(@Body('id') id: number, @UserInfo('userId') userId: number) {
    // return await this.commentService.like({id:id, userId:userId})
  }

  @RequireLogin()
  @Post('unlike')
  async unlike(@Body('id') id: number, @UserInfo('userId') userId: number) {
    // return await this.commentService.unlike({id:id,userId})
  }

  // @RequireLogin()
  @Post('comment')
  async comment(
    @Body() createCommentDto: CreateCommentDto,
    // @UserInfo() userInfo: any,
    @Req() req: Request,
  ) {
    try {
      const service: string =
        _BusinessTypeList[createCommentDto.type] + 'Service';
      await this.commentService.create(createCommentDto, req);
      return await this[service].commentIncrement(createCommentDto.createId);
    } catch (error) {
      throw new HttpException(error, HttpStatus.BAD_REQUEST)
    }
  }

  @RequireLogin()
  @Get('delete/:id')
  async removeComment(@Param('id') id: number) {
    return await this.commentService.remove(id);
  }
}
